package com.wangzemin.leetcoded.num1824;

public class Solution {
    public int minSideJumps(int[] obstacles) {
        if (obstacles == null || obstacles.length == 1) {
            return 0;
        }
        int[][] dp = new int[obstacles.length][3];
        for (int i=0;i<dp.length;i++){
            dp[i][0] = (int)(5*1e6);
            dp[i][1] = (int)(5*1e6);
            dp[i][2] = (int)(5*1e6);
        }

        dp[0][1] = 0;
        dp[0][0] = obstacles[0] == 1 ? Integer.MIN_VALUE : 1;
        dp[0][2] = obstacles[0] == 3 ? Integer.MIN_VALUE : 1;


        for (int i = 1; i < dp.length; i++) {
            if(obstacles[i] != 1) dp[i][0] = dp[i-1][0];
            if(obstacles[i] != 2) dp[i][1] = dp[i-1][1];
            if(obstacles[i] != 3) dp[i][2] = dp[i-1][2];
            if(obstacles[i] != 1) dp[i][0] = Math.min(dp[i][0], Math.min(dp[i][1], dp[i][2]) + 1);
            if(obstacles[i] != 2) dp[i][1] = Math.min(dp[i][1], Math.min(dp[i][0], dp[i][2]) + 1);
            if(obstacles[i] != 3) dp[i][2] = Math.min(dp[i][2], Math.min(dp[i][0], dp[i][1]) + 1);
        }

        return Math.min(dp[dp.length-1][0], Math.min(dp[dp.length-1][1], dp[dp.length-1][2]));
    }

//
//    public static void main(String[] args) {
//        Solution solution = new Solution();
//        System.out.println(solution.minSideJumps(new int[]{0,1,2,3,0}));
//    }
}